from setting.web import settings
import base64
import random
import uuid


def user_id():
    return ''.join(str(random.choice(range(10))) for _ in range(10))


def gen_id():
    return str(uuid.uuid4())


def build_file_path(file_name, type="events"):
    file_id = gen_id()

    file_path = settings.FILE_UPLOAD_DIR + \
        "{}/{}.{}".format(type, file_id, file_name)
    # 防止手抽
    if settings.FILE_UPLOAD_DIR[-1] != '/':
        file_path = settings.FILE_UPLOAD_DIR + \
            "/{}/{}.{}".format(type, file_id, file_name)

    file_path_with_no_prefix = file_path[2:]
    return (file_id, file_path, "/resource/{}/{}.{}".format(type, file_id, file_name))


def formatter(src: str, firstUpper: bool = False):
    """
    将下划线分隔的名字,转换为驼峰模式
    :param src:
    :param firstUpper: 转换后的首字母是否指定大写(如
    :return:
    """
    arr = src.split('_')
    res = ''
    for i in arr:
        res = res + i[0].upper() + i[1:]
    if not firstUpper:
        res = res[0].lower() + res[1:]
    return res

def text_to_ascii(name: str) -> str:
    text_bytes = name.encode('utf-8')  # 转换为字节
    base64_bytes = base64.b64encode(text_bytes)
    return base64_bytes.decode()

if __name__ == '__main__':
    print(uuid.uuid4())
